package com.venue.hjt.venueclass.fragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import com.venue.hjt.venueclass.MemberFingerprintAdapter
import com.venue.hjt.venueclass.R
import com.venue.hjt.venueclass.adapter.RecycleViewDivider
import com.venue.hjt.venueclass.db.SchArticle
import com.venue.hjt.venueclass.model.DongFModel
import com.venue.hjt.venueclass.utlis.Constant
import java.util.ArrayList

/**
 * Created by xqx on 2018/3/13.
 *
 */

class TabDongFragment : BaseFragment(){
    override fun initView(view: View?) {
        rvSeckillFingerprint = view!!.findViewById(R.id.rvSeckillFingerprint)
    }
    var model: DongFModel?=null
    var receiver:FDongReceiver?=null
    val list= ArrayList<SchArticle>()
    val list1= ArrayList<SchArticle>()
    var isNo:Boolean?=false
    var post=-1
    override fun loadData() {
        model= DongFModel()
        receiver=FDongReceiver()
        activity.registerReceiver(receiver,filter())
        val linearLayoutManager = LinearLayoutManager(activity)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        rvSeckillFingerprint!!.layoutManager = linearLayoutManager

        rvSeckillFingerprint!!.addItemDecoration(RecycleViewDivider(
                activity, LinearLayoutManager.VERTICAL, 11, resources.getColor(R.color.abc_background_cache_hint_selector_material_dark)))
        val pool1 = rvSeckillFingerprint!!.recycledViewPool
        pool1.setMaxRecycledViews(0, 0)
        rvSeckillFingerprint!!.recycledViewPool = pool1
//        val list=ArrayList<MemberFingerDataModel>()
        list+=model!!.schArticle
        list1+=model!!.schArticle
        memberFingerprintAdapter = MemberFingerprintAdapter(activity, list)
        memberFingerprintAdapter!!.setOnItemClickLitener({ _, position ->
            isNo=true
            if(post!=-1&&post!=position){
                list[post].setIsClose(false)
                memberFingerprintAdapter!!.notifyItemChanged(post)
            }
            memberFingerprintAdapter!!.setCurrentPosition(position)
            list[position].setIsClose(!list[position].isClose)
            post=position
            memberFingerprintAdapter!!.notifyItemChanged(position)
        })
        rvSeckillFingerprint!!.adapter = memberFingerprintAdapter
    }

    override fun getLayoutResource(): Int {
        return R.layout.fragment_dongtai
    }
    var memberFingerprintAdapter: MemberFingerprintAdapter? = null
    private var rvSeckillFingerprint: RecyclerView? = null
    /**
     * 广播类
     */
    inner class FDongReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            when (action) {
                Constant.ARTICLR->{
//                   Log.e("ATG","进来动态")
                    list.clear()
                    list1.clear()
                    list1+=model!!.schArticle
                    list+=model!!.schArticle
                    rvSeckillFingerprint!!.removeAllViews()
                    memberFingerprintAdapter = MemberFingerprintAdapter(activity, list)
                    rvSeckillFingerprint!!.adapter=memberFingerprintAdapter
                    memberFingerprintAdapter!!.setOnItemClickLitener({ _, position ->
                        memberFingerprintAdapter!!.setCurrentPosition(position)
                        list[position].setIsClose(!list[position].isClose)
                        memberFingerprintAdapter!!.notifyItemChanged(position)
                    })
                }
                Constant.SCHPOSTION->{
                    val position= intent.getIntExtra(Constant.SCHPOSTION,0)
                    list[position].setIsClose(true)
                    rvSeckillFingerprint!!.smoothScrollToPosition(position)
                    memberFingerprintAdapter!!.notifyItemChanged(position)
                }
            }
        }
    }
    /**
     * @return IntentFilter 广播
     */
    private fun filter(): IntentFilter {
        val filter = IntentFilter()
        filter.addAction(Constant.ARTICLR)
        filter.addAction(Constant.SCHPOSTION)
        return filter
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {

        if (isNo==true){
            Log.e("ATG","走到")
            for (i in 0 until list.size) {
                list[i].setIsClose(false)
            }
            memberFingerprintAdapter!!.notifyDataSetChanged()
        }

        super.setUserVisibleHint(isVisibleToUser)
    }

}